package titt.service.proxy;

import titt.service.content.NuderRequest;
import titt.service.content.NuderResponse;
import titt.service.invoke.AdvancedMethodInvoker;
import titt.type.Type;
import titt.type.TypedObject;

public class CombinedInvoker implements AdvancedMethodInvoker {

	private final AdvancedMethodInvoker nuder,titt;
	
	public CombinedInvoker(AdvancedMethodInvoker nuder, AdvancedMethodInvoker titt) {
		this.nuder = nuder;
		this.titt = titt;
	}
	
	@Override
	public TypedObject call(Type ret, String name, TypedObject... args) throws Exception {
		if(isNuder(ret))
			return nuder.call(ret, name, args);
		else
			return titt.call(ret, name, args);
	}

	@Override
	public TypedObject call(String name, TypedObject... args) throws Exception {
		if(isNuder(args[args.length-1].getType()))
			return nuder.call(name, args);
		else
			return titt.call(name, args);
	}
	
	private boolean isNuder(Type t) {
		String[] tokens = t.asString().split("\\.");
		String last = tokens[tokens.length-1];
		return last.equals(NuderResponse.class.getSimpleName()) || last.equals(NuderRequest.class.getSimpleName());
	}

}
